package logic

import (
	"context"
	"fmt"
	"muke-network/srv/model/mysql"

	"muke-network/srv/internal/svc"
	"muke-network/srv/muke"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsInfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsInfoLogic {
	return &GoodsInfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsInfoLogic) GoodsInfo(in *muke.GoodsInfoRequest) (*muke.GoodsInfoResponse, error) {
	// todo: add your logic here and delete this line

	g := mysql.Goods{}

	err := g.FindGoods(in.GoodsId)
	if err != nil {
		logx.Error("商品详情展示失败")
		return nil, fmt.Errorf("商品详情展示失败")
	}

	if g.ID == 0 {
		logx.Error("商品不存在")
		return nil, fmt.Errorf("商品不存在")
	}

	return &muke.GoodsInfoResponse{
		GoodsName:   g.GoodsName,
		GoodsCover:  g.GoodsCover,
		GoodsPrice:  float32(g.GoodsPrice),
		GoodsStock:  g.GoodsStock,
		GoodsStatus: g.GoodsStatus,
	}, nil
}
